home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / tde40.zip / config.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  41KB  |  1,153 lines

  1. /*
  2.  * This file contains the utilities to read in a ".tdecfg" file.
  3.  *
  4.  * Most of this stuff is duplicated from the cfgfile.c functions.  In
  5.  *  Linux, this utility searches the CWD first then it searches the
  6.  *  HOME directory for the ".tdecfg" file.
  7.  *
  8.  * Many thanks to <intruder@link.hacktic.nl> for the idea and sample code
  9.  *  for this function.
  10.  *
  11.  * New editor name:  TDE, the Thomson-Davis Editor.
  12.  * Author:           Frank Davis
  13.  * Date:             June 5, 1991, version 1.0
  14.  * Date:             July 29, 1991, version 1.1
  15.  * Date:             October 5, 1991, version 1.2
  16.  * Date:             January 20, 1992, version 1.3
  17.  * Date:             February 17, 1992, version 1.4
  18.  * Date:             April 1, 1992, version 1.5
  19.  * Date:             June 5, 1992, version 2.0
  20.  * Date:             October 31, 1992, version 2.1
  21.  * Date:             April 1, 1993, version 2.2
  22.  * Date:             June 5, 1993, version 3.0
  23.  * Date:             August 29, 1993, version 3.1
  24.  * Date:             November 13, 1993, version 3.2
  25.  * Date:             June 5, 1994, version 4.0
  26.  *
  27.  * This code is released into the public domain, Frank Davis.
  28.  * You may distribute it freely.
  29.  */
  30.  
  31.  
  32. #include "tdestr.h"             /* tde types */
  33. #include "common.h"
  34. #include "define.h"
  35. #include "tdefunc.h"
  36.  
  37. #if defined(  __UNIX__ )
  38.    #include "cfgfile.h"
  39. #endif
  40.  
  41.  
  42. #if !defined( __UNIX__ )
  43.  #include <dos.h>               /* for renaming files */
  44.  #include <bios.h>              /* for direct BIOS keyboard input */
  45.  #include <io.h>                /* for file attribute code */
  46.  #if defined( __MSC__ )
  47.   #include <errno.h>
  48.   #include <sys\types.h>        /* S_IWRITE etc */
  49.  #endif
  50.  #include <sys\stat.h>          /* S_IWRITE etc */
  51. #endif
  52.  
  53. #include <fcntl.h>              /* open flags */
  54.  
  55.  
  56. #if defined( __UNIX__ )
  57. char *line_in;                  /* line buffer */
  58. char *line_out;                 /* line buffer */
  59. int  stroke_count;              /* global variable for macro strokes */
  60. unsigned int line_no;           /* global variable for line count */
  61. int  need_a_redraw;             /* if we redefined colors, then redraw screen */
  62. int  need_mode_line;            /* if we redefined modes, then redraw line */
  63. int  need_rulers;               /* if we redefined rulers, then redraw ruler */
  64.  
  65.  
  66. /*
  67.  * UNIX stuff:  let's make us an array of colors used in curses.
  68.  */
  69. int curse_col[8] = { COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
  70.                      COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE };
  71. #endif
  72.  
  73. /*
  74.  * Name:    tdecfgfile
  75.  * Date:    June 5, 1994
  76.  * Notes:   read in a configuration file at any time.
  77.  *          read in a configuration file when we first fire up TDE in
  78.  *           a linux (unix) environment.
  79.  */
  80. int  tdecfgfile( TDE_WIN *window )
  81. {
  82. FILE *config;
  83. int  rc;
  84. int  prompt_line;
  85. int *clr;
  86. #if defined( __UNIX__ )
  87.  char fname[PATH_MAX];          /* new name for file  */
  88.  char *home;
  89. #else
  90.  char fname[MAX_COLS];          /* new name for file */
  91. #endif
  92.  
  93.    rc = OK;
  94.  
  95. #if defined( __UNIX__ )
  96.    if (window != NULL)
  97.       prompt_line = window->bottom_line;
  98.    else
  99.       prompt_line = g_display.nlines;
  100.  
  101.    /*
  102.     * first, make sure we can alloc space for line buffers.
  103.     *  line buffers are needed for reading the config file.
  104.     */
  105.    line_in = my_malloc( MAX_LINE_LENGTH - 1, &rc );
  106.    if (rc == OK)
  107.       line_out = my_malloc( g_display.ncols + 2, &rc );
  108.    else
  109.       line_out = NULL;
  110.  
  111.    if (rc == OK) {
  112.       /*
  113.        * prompt for the configuration file name.
  114.        */
  115. #if defined( __UNIX__ )
  116.  
  117.       /*
  118.        * in Linux, we search for a ".tdecfg" file in
  119.        *   1) current working directory == "."
  120.        *   2) the user's home directory.
  121.        *
  122.        * CONFIGFILE is defined in tdestr.h
  123.        */
  124.       strcpy( fname, "." );
  125.       strcat( fname, "/" );
  126.       strcat( fname, CONFIGFILE );
  127.       if (access( fname, F_OK ) != 0) {
  128.  
  129.          /*
  130.           * could not find config file in cwd.  try user's home directory.
  131.           */
  132.          home = (char *)getenv( "HOME" );
  133.          if (home == NULL)
  134.             rc = ERROR;
  135.          else {
  136.             strcpy( fname, home );
  137.             strcat( fname, "/" );
  138.             strcat( fname, CONFIGFILE );
  139.          }
  140.       }
  141. #else
  142.       *fname = '\0';
  143.       rc = get_name( config1, prompt_line, fname, g_display.message_color );
  144. #endif
  145.  
  146.       if (rc == OK) {
  147.          if ((config = fopen( fname, "r" )) == NULL) {
  148.             rc = ERROR;
  149.             if (window != NULL) {
  150.                combine_strings( line_out, main7a, fname, main7b );
  151.                error( WARNING, prompt_line, line_out );
  152.             }
  153.          }
  154.  
  155.          /*
  156.           * if everything is everthing so far, get the current editor settings.
  157.           */
  158.          if (rc == OK) {
  159.  
  160.             need_a_redraw = FALSE;
  161.             need_mode_line = FALSE;
  162.             need_rulers = FALSE;
  163.  
  164.             stroke_count = get_stroke_count( );
  165.  
  166.             line_no = 1;
  167.             while (!feof( config )) {
  168.                if (fgets( line_in, 1500, config ) == NULL)
  169.                   break;
  170.  
  171.                /*
  172.                 * for convenience, let's remove the <cr><lf> pair from
  173.                 *   from MSDOS-type text files.
  174.                 */
  175. #if defined( __UNIX__ )
  176.                remove_cr( line_in );
  177. #endif
  178.                parse_line( line_in, prompt_line );
  179.                ++line_no;
  180.             }
  181.  
  182.             fclose( config );
  183.  
  184.             if (need_a_redraw  &&  g_display.adapter != MDA) {
  185.                clr = &colour.clr[1][0];
  186.                g_display.head_color    = *clr++;
  187.                g_display.text_color    = *clr++;
  188.                g_display.dirty_color   = *clr++;
  189.                g_display.mode_color    = *clr++;
  190.                g_display.block_color   = *clr++;
  191.                g_display.message_color = *clr++;
  192.                g_display.help_color    = *clr++;
  193.                g_display.diag_color    = *clr++;
  194.                g_display.eof_color     = *clr++;
  195.                g_display.curl_color    = *clr++;
  196.                g_display.ruler_color   = *clr++;
  197.                g_display.ruler_pointer = *clr++;
  198.                g_display.hilited_file  = *clr++;
  199.                g_display.overscan      = *clr;
  200.                if (window != NULL)
  201.                   redraw_screen( window );
  202.             }
  203.             if (need_mode_line  &&  window != NULL)
  204.                show_modes( );
  205.             if (need_rulers  &&  window != NULL)
  206.                show_all_rulers( );
  207.          }
  208.       }
  209.    } else {
  210.       /*
  211.        * not enough memory
  212.        */
  213.       error( WARNING, prompt_line, main4 );
  214.       rc = ERROR;
  215.    }
  216.  
  217.    if (line_in != NULL)
  218.       my_free( line_in );
  219.    if (line_out != NULL)
  220.       my_free( line_out );
  221. #endif
  222.    return( rc );
  223. }
  224.  
  225.  
  226.  
  227. #if defined( __UNIX__ )
  228.  
  229. /*
  230.  * Name:    remove_cr
  231.  * Purpose: get rid of <cr>
  232.  * Date:    June 5, 1994
  233.  * Passed:  line:  line of text
  234.  * Notes:   UNIX don't like <cr><lf>.  if we read MSDOS config files,
  235.  *            we need to get rid of the <cr><lf> thing.  change <cr> to <lf>.
  236.  */
  237. void remove_cr( char *line )
  238. {
  239.    if (line != NULL) {
  240.       while (*line) {
  241.          if (*line == '\r')
  242.             *line = '\n';
  243.          ++line;
  244.       }
  245.    }
  246. }
  247.  
  248.  
  249. /*
  250.  * Name:    parse_line
  251.  * Purpose: real work horse of the configuration utility, figure out what
  252.  *          we need to do with each line of the config file.
  253.  * Date:    June 5, 1994
  254.  * Passed:  line:  line that contains the text to parse
  255.  */
  256. void parse_line( char *line, int prompt_line )
  257. {
  258. char key[1042];         /* buffer to hold any token that we parse */
  259. char *residue;          /* pointer to next item in line, if it exists */
  260. int key_no;             /* index into key array */
  261. int parent_key;         /* 1st of two-combination keys */
  262. int color;              /* color field */
  263. int mode_index;         /* index in mode array */
  264. int func_no;            /* function number we want to assign to a key */
  265. int color_no;           /* attribute we want to assign